% function cascadeFineTuning3
clear;clc;
global bForceUpdate
bForceUpdate = true;
% bForceUpdate = false;

ModelHomePath = srpath.getResultPath('CascadeFineTuning2');

%%
cmd.caffe = mycaffe.caffe_cmd('c1');
cmd.solver_mode = 'gpu 0';

%% set train & test
Patch.scales = 3;
Patch.x_size = 41;
Patch.y_size = 41;
Patch.stride = 14;
Patch.batchsize = 64;

Aug.angles = 1 : 4;
Aug.scales = .6 : .1 : .7;
Aug.flip_dims = [];

Train.files = srdata.getH5File('NaiveTrainImg', Patch, Aug);
Test.files = srdata.getH5File('Set5', Patch);

Train.batch_size = 64;
Test.batch_size = 2;

%% set solver param
solv = SR_Exp.getSolv;

%% set net param
way = 'y';
net.kernel_size = 3;

net.weight_init_type = 'msra';
net.result_path = ModelHomePath;
net = VDSR.getNet(way, net);

%%
solv.max_iter = 500;
% cmd = rmfield(cmd, 'snapshot');

% for i = 1 : 2
%     mynet.name = 'VDSR_y';
%     net.folder = '[VDSR_y-1]-tuning2-[VDSR_y-1]';
%     mynet.deploy_file = fullfile(net.result_path0, net.folder, 'VDSR_y_deploy.prototxt');
%     cmd.weights = fullfile(net.result_path0, net.folder, 'models', 'VDSR_y_iter_500.caffemodel');
%     [Train.files, Test.files] = srimg.deployTrainTest(Train.files, Test.files, cmd.solver_mode, mynet.name, mynet.deploy_file, cmd.weights);
%     
%     [mynet, TrainingCmd] = mycaffe.genTrainingCmd(net, solv, cmd, Train, Test);
%     system(TrainingCmd);
% end

%%
Net_Models = {
    'VDSR_y-1', 'VDSR_y_iter_500.caffemodel'
    '[VDSR_y-1]-tuning2-[VDSR_y-1]', 'VDSR_y_iter_500.caffemodel'
%     '[VDSR_y-1]-tuning2-[VDSR_y-1]', 'VDSR_y_iter_500.caffemodel'
    };

bMandatoryUpdate = false;
forward_batch_size = 200;

for i = 1 : size(Net_Models, 1)
    NetPath = fullfile(ModelHomePath, Net_Models{i, 1});
    deploy_file = fullfile(NetPath, 'VDSR_y_deploy.prototxt');
    caffemodel  = fullfile(NetPath, 'models', Net_Models{i, 2});
    [Train.files, Test.files] = srdata.getDeployedTrainTest(Train.files, Test.files, cmd.solver_mode, deploy_file, caffemodel, bMandatoryUpdate, forward_batch_size);
end

cmd.weights = caffemodel;
[mynet, TrainingCmd] = mycaffe.genTrainingCmd(net, solv, cmd, Train, Test);
system(TrainingCmd);
% mynet.result_path
% pause
%%
scale = 3;
interval = 100;

ImgPairs = srimg.genLHPairs('Set5', scale);
ImgPairs = ImgPairs(:, 2:3);

%%
psnrList = cell(1, size(Net_Models, 1)+1);
for i = 1 : size(Net_Models, 1)
    NetPath = fullfile(ModelHomePath, Net_Models{i, 1});
    deploy_file = fullfile(NetPath, 'VDSR_y_deploy.prototxt');
    caffemodel  = fullfile(NetPath, 'models', Net_Models{i, 2});
    [ImgPairs, psnrList{i}] = srmodel.deployPairs(ImgPairs, scale, cmd.solver_mode, deploy_file, caffemodel);
end

mynet.model_prefix = [mynet.name '_iter_'];

[~, psnrList{end}] = srdata.deployImgsByBatchModels(ImgPairs, scale, mynet, [100, interval, 500]);

%%
% myinput.save(psnrList);

%%
meanpsnrList = cellfun(@mean, psnrList, 'uniformoutput', false);
for i = 1 : length(meanpsnrList)
%     fprintf('%d  ', net_iters{i, 2} : interval : net_iters{i, 3});
%     fprintf('\n')
    fprintf('%.2f ', meanpsnrList{i});
    fprintf('\n============================\n')
end

%%
disp(mynet.result_path)

